package org.uma.kri.nyu.mor.almas.util;

import java.util.List;
import java.util.Map;

import org.uma.kri.nyu.mor.almas.R;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.BufferType;

/**
 * 編集リスト項目アダプター
 * 
 * @author kri
 * 
 */
public class EditBaseAdapter extends BaseExpandableListAdapter {

	public final static String KEY_GROUP = "GROUP";
	public final static String KEY_PHONE = "PHONE";
	public final static String KEY_EMAIL = "EMAIL";
	public final static String KEY_TITLE = "TITLE";
	public final static String KEY_SUMMARY = "SUMMARY";
	

	// メンバ変数
	private List<String> _group;
	private List<List<Map<String, String>>> _child;
	private Context _context;
	
	public EditBaseAdapter(Context context, List<String> group, List<List<Map<String, String>>> child) {
		_context = context;
		_group = group;
		_child = child;
	}
	
	public View getGenericView (int groupIndex) {
		switch(groupIndex) {
		case 0:
		case 1:
			return LayoutInflater.from(_context).inflate(R.layout.expandablelist_child, null);
		case 2:
			return LayoutInflater.from(_context).inflate(R.layout.expandablelist_event, null);
		default:
		return null;
		}
	}
	
    public TextView getGroupGenericView() {
    	
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64); 
        
        TextView textView = new TextView(_context);
        textView.setLayoutParams(param);
         
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(64, 0, 0, 0);
         
        return textView;
    }
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return _child.get(groupPosition).get(childPosition);
	}
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		Resources res = _context.getResources();
		// ExpandableListViewのレイアウトを生成
		View view = getGenericView(groupPosition);		
		
		if (groupPosition < 2) {
			// 電話番号・メールアドレスの設定
			EditText info = (EditText)view.findViewById(R.id.editInfo_Row_Edit);
			// keyによってListViewへの登録情報を判定
			info.setText(_child.get(groupPosition).get(childPosition).get(KEY_SUMMARY), BufferType.NORMAL);
		} else if (groupPosition == 2) {
			TextView info = (TextView)view.findViewById(R.id.editInfo_RowName_Text);
			// keyによってListViewへの登録情報を判定
			info.setText(_child.get(groupPosition).get(childPosition).get(KEY_SUMMARY), BufferType.NORMAL);
			info.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO 自動生成されたメソッド・スタブ
					// DatePickerダイアログ表示イベントをセット
				}
			});
		}
		// return super.getView(position, convertView, parent);
		return view;
		}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		return _child.get(groupPosition).size();
	}
	@Override
	public Object getGroup(int groupPosition) {
		return _group.get(groupPosition);
	}
	@Override
	public int getGroupCount() {
		return _child.size();
	}
	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView textView = getGroupGenericView();
		textView.setText(_group.get(groupPosition), BufferType.NORMAL);

		return textView;
	}
	@Override
	public boolean hasStableIds() {
		return true;
	}
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	// 日付設定イベント（ダイアログから設定）
	DatePickerDialog.OnDateSetListener datePickerDialog = new OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			
		}
	};
}
